from universal.common_header import *
from universal.common_header_widget import *
from mylibs.lib import addActions,generateColorByText,struct
from Module.xRayDetection.xray.logs import Logger
from universal.translate.CHINESE import *


class Video(QWidget):

    def __init__(self,parent=None):
        super(Video, self).__init__(parent=parent)
        self.mainwindow = parent
        videoLayout = QVBoxLayout(self)
        self.VideoTime = QComboBox()
        self.time = ['0.1s', '0.2s', '0.25s', '0.3s', '0.35s', '0.4s', '0.5s', '1s', '1.5s', '2s', '2.5s', '3s', '4s',
                     '5s']
        self.time1 = ['100', '200', ' 250', ' 300', '350', '400', '500', '1000', '1500', '2000', '2500', '3000', '4000',
                      '5000']
        self.VideoTime.addItems(self.time)
        self.startVideo = QPushButton('开始播放')
        self.closeVideo = QPushButton('停止播放')

        self.startVideo.setStyleSheet('''QPushButton{background:rgb(75,129,209);
                              border-radius:5px;}QPushButton:hover{background:rgb(175,99,186);}''')
        self.closeVideo.setStyleSheet('''QPushButton{background:rgb(75,129,209);
                              border-radius:5px;}QPushButton:hover{background:rgb(175,99,186);}''')
        videoLayout.addWidget(self.VideoTime)
        videoLayout.addWidget(self.startVideo)
        videoLayout.addWidget(self.closeVideo)
        videoLayout.addSpacing(500)

        self.valueGO = True
        self.valueGO_left = True

        self.timerRight = QTimer()
        self.timerRight.timeout.connect(self.mainwindow.openNextImg)
        self.timerLeft = QTimer()
        self.timerLeft.timeout.connect(self.mainwindow.openPrevImg)
        self.startVideo.clicked.connect(lambda: self.Start_Video(True))
        self.closeVideo.clicked.connect(lambda: self.Start_Video(False))

    def Start_Video(self, value_):
        if value_ and self.valueGO == True:
            str_ = self.VideoTime.currentIndex()
            str_ = self.time1[str_]
            self.timerRight.start(int(str_))
            self.valueGO = False
        elif value_ == False:
            self.timerRight.stop()
            self.valueGO = True

    def Start_Video_Left(self, value_):
        if value_ and self.valueGO_left == True:
            str_ = self.VideoTime.currentIndex()
            str_ = self.time1[str_]
            self.timerLeft.start(int(str_))
            self.valueGO_left = False
        elif value_ == False:
            self.timerLeft.stop()
            self.valueGO_left = True

class LabelTab(QWidget):
    def __init__(self,parent=None):
        super(LabelTab, self).__init__(parent=parent)
        self.mainwindow = parent

        self.labelList = Listwidget(self.mainwindow)  # 显示当前类别
        self.dock = QDockWidget(self.mainwindow)  # 显示当前类别DOCK
        self.dock.setTitleBarWidget(QWidget())
        self.dock.setWidget(self.labelList)

        self.fileListWidget = Listwidget(self.mainwindow)
        self.fileListWidget.Listwidget_color()

        self.LineEdit_picture = QLineEdit()
        self.LineEdit_picture.textChanged.connect(self.LineEvent)
        self.LineEdit_picture.setFont(QFont("Arial", 11))
        self.LineEdit_picture.setPlaceholderText(SEARCH_NAME)
        self.Upload = Button(UPLOAD_FTP)

        self.TextBrowser = TextBrowserBulid()  # 消息框
        self.labelLayouContainer = QWidget()

        self.Tablewidget_img()  # 界面布局  ---TableImg

        LabelLayout = QVBoxLayout(self.labelLayouContainer)
        LabelLayout.addWidget(self.TabelParameter_Input)  ####
        LabelLayout.addWidget(self.dock)
        LabelLayout.addWidget(self.LineEdit_picture)
        LabelLayout.addWidget(self.fileListWidget)
        LabelLayout.addWidget(self.Upload)
        LabelLayout.addWidget(self.TextBrowser)


    def Tablewidget_img(self):
        self.Table = Mainwindow_Label_table(3, 2, ["图像路径", "半自动标注路径", '当前类别'], self.mainwindow)
        self.imgspathLineEdit, self.CurrentLabel, self.TabelParameter_Input,self.HalfAuto = self.Table.widget()

    def returnParameter(self):
        return self.labelList ,self.dock,self.fileListWidget, self.TextBrowser,self.Upload


    def LineEvent(self):
        list = []
        self.fileListWidget.clear()
        pictureName = self.LineEdit_picture.text()
        for imgPath in self.mainwindow.mImgList:
            iconName = os.path.basename(imgPath)
            if pictureName in iconName:
                self.fileListWidget_show(imgPath)
                list.append(imgPath)
        self.mainwindow.compressPicPath_ = list


    def fileListWidget_show(self, imgPath):
        iconName = os.path.basename(imgPath)
        totals = len(self.mainwindow.mImgList)
        self.idx_totalList = self.mainwindow.mImgList.index(str(imgPath)) + 1
        idx_iconName = "第%s/%s—>%s" % (self.idx_totalList, totals, iconName)
        QListWidgetItem(str(idx_iconName), self.fileListWidget).setForeground(QColor(50, 255, 255))
        self.fileListWidget.setMovement(QListView.Static)
        self.fileListWidget.setResizeMode(QListView.Adjust)
        self.fileListWidget.setSpacing(1)
